package com.xingqing.controller;

import com.xingqing.domain.dto.EmpDTO;
import com.xingqing.domain.entity.Emp;
import com.xingqing.domain.vo.EmpVO;
import com.xingqing.domain.vo.PageBean;
import com.xingqing.domain.vo.Result;
import com.xingqing.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping
    public Result<PageBean<EmpVO>> page(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询员工信息，页码：{}，页大小：{}", page, pageSize);
        PageBean<EmpVO> pageBean = empService.pageWithDept(page, pageSize);
        log.info("查询到{}条记录，共{}页", pageBean.getTotal(), 
                (pageBean.getTotal() + pageSize - 1) / pageSize);
        return Result.success(pageBean);
    }

    @GetMapping("/list")
    public Result<List<EmpVO>> list() {
        log.info("查询所有员工信息(包含部门)");
        List<EmpVO> empList = empService.listWithDept();
        log.info("查询到{}个员工", empList.size());
        return Result.success(empList);
    }

    @GetMapping("/{id}")
    public Result<EmpVO> getById(@PathVariable Long id) {
        log.info("根据ID查询员工信息(包含部门)，ID: {}", id);
        EmpVO emp = empService.getByIdWithDept(id);
        log.info("查询到员工信息: {} - {}", emp != null ? emp.getName() : "null", 
                emp != null ? emp.getDeptName() : "null");
        return Result.success(emp);
    }

    @GetMapping("/dept/{deptId}")
    public Result<List<EmpVO>> getByDeptId(@PathVariable Long deptId) {
        log.info("根据部门ID查询员工信息(包含部门)，部门ID: {}", deptId);
        List<EmpVO> empList = empService.getByDeptIdWithDept(deptId);
        log.info("查询到{}个员工", empList.size());
        return Result.success(empList);
    }

    @PostMapping
    public Result<Void> save(@RequestBody Emp emp) {
        log.info("新增员工: {}", emp.getName());
        empService.save(emp);
        log.info("员工新增成功");
        return Result.success();
    }

    @PostMapping("/with-experiences")
    public Result<Void> saveWithWorkExperiences(@RequestBody EmpDTO empDTO) {
        log.info("新增员工及工作经历: {}", empDTO.getName());
        empService.saveWithWorkExperiences(empDTO);
        log.info("员工及工作经历新增成功");
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @RequestBody Emp emp) {
        log.info("更新员工信息，ID: {}, 新姓名: {}", id, emp.getName());
        emp.setId(id);
        empService.updateById(emp);
        log.info("员工更新成功");
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        log.info("删除员工，ID: {}", id);
        empService.deleteById(id);
        log.info("员工删除成功");
        return Result.success();
    }
}